<template>
  <div>
    <h3>医保机构管理</h3>

    <!-- 查询功能 -->
    <el-input v-model="searchQuery" placeholder="请输入机构名称或类型进行查询" style="width: 300px; margin-bottom: 20px;" clearable></el-input>
    <el-button type="primary" @click="fetchMedicalInstitutions">查询</el-button>

    <!-- 添加医保机构按钮 -->
    <el-button type="primary" @click="openDialog(null)">添加医保机构</el-button>

    <!-- 医保机构列表 -->
    <el-table :data="filteredInstitutions" style="width: 100%">
      <el-table-column prop="medicalinstitutionid" label="医保机构ID" width="180"></el-table-column>
      <el-table-column prop="institutionname" label="机构名称" width="180"></el-table-column>
      <el-table-column prop="institutiontype" label="机构类型" width="120"></el-table-column>
      <el-table-column prop="address" label="地址" width="200"></el-table-column>
      <el-table-column prop="contactnumber" label="联系电话" width="150"></el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteMedicalInstitution(scope.row.medicalinstitutionid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑医保机构对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible">
      <el-form :model="institutionForm" label-width="120px">
        <el-form-item label="医保机构ID">
          <el-input v-model="institutionForm.medicalinstitutionid" :disabled="isEditing"></el-input>
        </el-form-item>
        <el-form-item label="机构名称">
          <el-input v-model="institutionForm.institutionname"></el-input>
        </el-form-item>
        <el-form-item label="机构类型">
          <el-select v-model="institutionForm.institutiontype" placeholder="请选择类型">
            <el-option label="医院" value="1"></el-option>
            <el-option label="诊所" value="2"></el-option>
            <el-option label="药店" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="institutionForm.address"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="institutionForm.contactnumber"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveMedicalInstitution">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      searchQuery: '', // 搜索关键词
      institutionList: [],
      institutionForm: {
        medicalinstitutionid: '',
        institutionname: '',
        institutiontype: '',
        address: '',
        contactnumber: ''
      },
      dialogVisible: false,
      dialogTitle: '',
      isEditing: false
    };
  },
  computed: {
    // 根据机构名称或机构类型筛选医保机构
    filteredInstitutions() {
      return this.institutionList.filter(institution =>
          institution.institutionname.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
          institution.institutiontype.includes(this.searchQuery)
      );
    }
  },
  mounted() {
    this.fetchMedicalInstitutions()
  },
  methods: {
    // 获取医保机构列表
    fetchMedicalInstitutions() {
      axios.get('http://localhost:8081/medicalInstitution/all')
          .then(response => {
            this.institutionList = response.data
          })
          .catch(error => {
            console.error("Error fetching insured persons:", error);
          });
    },
    // 打开添加/编辑对话框
    openDialog(row) {
      if (row) {
        this.isEditing = true;
        this.dialogTitle = '编辑医保机构';
        this.institutionForm = {...row};
      } else {
        this.isEditing = false;
        this.dialogTitle = '添加医保机构';
        this.resetForm();
      }
      this.dialogVisible = true;
    },
    // 重置表单
    resetForm() {
      this.institutionForm = {
        medicalinstitutionid: '',
        institutionname: '',
        institutiontype: '',
        address: '',
        contactnumber: ''
      };
    },
    // 保存医保机构
    saveMedicalInstitution() {
      if (this.isEditing) {
        axios.put(`http://localhost:8081/medicalInstitution/update`, this.institutionForm)
            .then(() => {
              this.fetchMedicalInstitutions();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error updating insured person:", error);
            });
      } else {
        axios.post('http://localhost:8081/medicalInstitution/add', this.institutionForm)
            .then(() => {
              this.fetchMedicalInstitutions();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error adding insured person:", error);
            });
      }
    },
    // 删除医保机构
    deleteMedicalInstitution(medicalinstitutionid) {
      axios.delete(`http://localhost:8081/medicalInstitution/delete/${medicalinstitutionid}`)
          .then(() => {
            this.fetchMedicalInstitutions();
          })
          .catch(error => {
            console.error("Error deleting insured person:", error);
          });
    }
  }
};
</script>

<style scoped>
.el-button {
  margin-bottom: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
